using System;
using System.Data;
using System.Data.SqlClient;

using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TradeAccountingDataBase.
	/// </summary>
	public class TMSDataBase {

		protected DataSet ds = new DataSet();
		protected CNCSoft.EData.VoucherNoGenerator vg;
        protected CNCSoft.EWinform.ERPApps.WorkingSessionInfo WSInfo = GlobalVariable.WSInfo;  //Coi chung

		public TMSDataBase() {
            vg = new VoucherNoGenerator(GlobalVariable.WSInfo.ConnectionString, GlobalVariable.WSInfo.WorkingDate);
			vg.VoucherNoFormatDesign = CNCSoft.EData.VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_DD_Delimiter_SerialNo;
			vg.EndingFormat = "000";
		}		

		public DataSet getDataSet() {
			return ds;
		}



        protected string _cnnString = GlobalVariable.WSInfo.ConnectionString;

		protected class SimpleMasterDetailUpdater {

			public SimpleMasterDetailUpdater(
				string mSelectCommand,
				string dSelectCommand,
				DataTable mtable,
				DataTable dtable,
				string cnnString) {

				SqlConnection sqlcn = new SqlConnection(cnnString);
				sqlcn.Open();
				SqlTransaction sqltran = sqlcn.BeginTransaction();

				SqlDataAdapter sqldaM = new SqlDataAdapter(mSelectCommand, sqlcn);
				SqlDataAdapter sqldaD = new SqlDataAdapter(dSelectCommand, sqlcn);
				try {
					DataHelper.buildDataCommands(ref sqldaM, ref sqltran);
					DataHelper.buildDataCommands(ref sqldaD, ref sqltran);

					DataTable dt; // data changed
                    /*
                    dt = dtable.GetChanges();
                    if (dt != null)  sqldaD.Update(dt);
                    dt = mtable.GetChanges();
                    if (dt != null) sqldaM.Update(dt);
                    */

					// Case: Delete
					dt = dtable.GetChanges(DataRowState.Deleted);
					if (dt != null) sqldaD.Update(dt);
					dt = mtable.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaM.Update(dt);
					// Case: Add new
					dt = mtable.GetChanges(DataRowState.Added);
					if (dt != null) sqldaM.Update(dt);
					dt = dtable.GetChanges(DataRowState.Added);
					if (dt != null) sqldaD.Update(dt);
					// Case: Modify
					dt = mtable.GetChanges(DataRowState.Modified);
					if (dt != null) sqldaM.Update(dt);
					dt = dtable.GetChanges(DataRowState.Modified);
					if (dt != null) sqldaD.Update(dt);
                    
					sqltran.Commit();
				} catch (Exception ex) {
					try {
						sqltran.Rollback();
					} catch {}
					throw ex;
				} finally {
					try {
						sqltran.Rollback();
						sqlcn.Close();
						sqltran.Dispose();
						sqlcn.Dispose();
						sqldaM.Dispose();
						sqldaD.Dispose();
					} catch {}
				}
			}
		}

        //----------LONGNT-----------------------------
        protected class SimpleMasterDetailUpdater1
        {

            public SimpleMasterDetailUpdater1(
                string mSelectCommand,
                string dSelectCommand1,
                string dSelectCommand2,
                string dSelectCommand3,
                DataTable mtable,
                DataTable dtable1,
                DataTable dtable2,
                DataTable dtable3,
                string cnnString)
            {

                SqlConnection sqlcn = new SqlConnection(cnnString);
                sqlcn.Open();
                SqlTransaction sqltran = sqlcn.BeginTransaction();

                SqlDataAdapter sqldaM = new SqlDataAdapter(mSelectCommand, sqlcn);
                SqlDataAdapter sqldaD1 = new SqlDataAdapter(dSelectCommand1, sqlcn);
                SqlDataAdapter sqldaD2 = new SqlDataAdapter(dSelectCommand2, sqlcn);
                SqlDataAdapter sqldaD3 = new SqlDataAdapter(dSelectCommand3, sqlcn);
                try
                {
                    DataHelper.buildDataCommands(ref sqldaM, ref sqltran);
                    DataHelper.buildDataCommands(ref sqldaD1, ref sqltran);
                    DataHelper.buildDataCommands(ref sqldaD2, ref sqltran);
                    DataHelper.buildDataCommands(ref sqldaD3, ref sqltran);

                    DataTable dt; // data changed
                    /*
                    dt = dtable.GetChanges();
                    if (dt != null)  sqldaD.Update(dt);
                    dt = mtable.GetChanges();
                    if (dt != null) sqldaM.Update(dt);
                    */

                    // Case: Delete
                    dt = dtable1.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaD2.Update(dt);
                    dt = dtable3.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaD3.Update(dt);
                    dt = mtable.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaM.Update(dt);
                    // Case: Add new
                    dt = mtable.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaM.Update(dt);
                    dt = dtable1.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaD2.Update(dt);
                    dt = dtable3.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaD3.Update(dt);
                    // Case: Modify
                    dt = mtable.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaM.Update(dt);
                    dt = dtable1.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaD2.Update(dt);
                    dt = dtable3.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaD3.Update(dt);

                    sqltran.Commit();
                }
                catch (Exception ex)
                {
                    try
                    {
                        sqltran.Rollback();
                    }
                    catch { }
                    throw ex;
                }
                finally
                {
                    try
                    {
                        sqltran.Rollback();
                        sqlcn.Close();
                        sqltran.Dispose();
                        sqlcn.Dispose();
                        sqldaM.Dispose();
                        sqldaD1.Dispose();
                        sqldaD2.Dispose();
                        sqldaD3.Dispose();
                    }
                    catch { }
                }
            }

            public SimpleMasterDetailUpdater1(
                string mSelectCommand,
                string dSelectCommand1,
                string dSelectCommand2,
                DataTable mtable,
                DataTable dtable1,
                DataTable dtable2,
                string cnnString)
            {

                SqlConnection sqlcn = new SqlConnection(cnnString);
                sqlcn.Open();
                SqlTransaction sqltran = sqlcn.BeginTransaction();

                SqlDataAdapter sqldaM = new SqlDataAdapter(mSelectCommand, sqlcn);
                SqlDataAdapter sqldaD1 = new SqlDataAdapter(dSelectCommand1, sqlcn);
                SqlDataAdapter sqldaD2 = new SqlDataAdapter(dSelectCommand2, sqlcn);
                try
                {
                    DataHelper.buildDataCommands(ref sqldaM, ref sqltran);
                    DataHelper.buildDataCommands(ref sqldaD1, ref sqltran);
                    DataHelper.buildDataCommands(ref sqldaD2, ref sqltran);

                    DataTable dt; // data changed
                    /*
                    dt = dtable.GetChanges();
                    if (dt != null)  sqldaD.Update(dt);
                    dt = mtable.GetChanges();
                    if (dt != null) sqldaM.Update(dt);
                    */

                    // Case: Delete
                    dt = dtable1.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaD2.Update(dt);
                    dt = mtable.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaM.Update(dt);
                    // Case: Add new
                    dt = mtable.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaM.Update(dt);
                    dt = dtable1.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaD2.Update(dt);
                    // Case: Modify
                    dt = mtable.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaM.Update(dt);
                    dt = dtable1.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaD1.Update(dt);
                    dt = dtable2.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaD2.Update(dt);

                    sqltran.Commit();
                }
                catch (Exception ex)
                {
                    try
                    {
                        sqltran.Rollback();
                    }
                    catch { }
                    throw ex;
                }
                finally
                {
                    try
                    {
                        sqltran.Rollback();
                        sqlcn.Close();
                        sqltran.Dispose();
                        sqlcn.Dispose();
                        sqldaM.Dispose();
                        sqldaD1.Dispose();
                        sqldaD2.Dispose();
                    }
                    catch { }
                }
            }
        }
        //---------=ENDLONGNT--------------------------



        protected class SimpleMasterUpdater
        {

            public SimpleMasterUpdater(
                string mSelectCommand,
                DataTable mtable,
                string cnnString)
            {

                SqlConnection sqlcn = new SqlConnection(cnnString);
                sqlcn.Open();
                SqlTransaction sqltran = sqlcn.BeginTransaction();

                SqlDataAdapter sqldaM = new SqlDataAdapter(mSelectCommand, sqlcn);
                try
                {
                    DataHelper.buildDataCommands(ref sqldaM, ref sqltran);

                    DataTable dt; // data changed

                    // Case: Delete
                    dt = mtable.GetChanges(DataRowState.Deleted);
                    if (dt != null) sqldaM.Update(dt);
                    // Case: Add new
                    dt = mtable.GetChanges(DataRowState.Added);
                    if (dt != null) sqldaM.Update(dt);
                    // Case: Modify
                    dt = mtable.GetChanges(DataRowState.Modified);
                    if (dt != null) sqldaM.Update(dt);

                    sqltran.Commit();
                }
                catch (Exception ex)
                {
                    try
                    {
                        sqltran.Rollback();
                    }
                    catch { }
                    throw ex;
                }
                finally
                {
                    try
                    {
                        sqltran.Rollback();
                        sqlcn.Close();
                        sqltran.Dispose();
                        sqlcn.Dispose();
                        sqldaM.Dispose();
                    }
                    catch { }
                }
            }
        }


	}
}
